package com.mycookbook;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CategoryActivity extends Activity
{
	DatabaseHandler db;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.categorylayout);
        
        //get database context
        ApplicationData d = ((ApplicationData)getApplicationContext());
        
        db = d.GetDatabase();
        
        //Initializing objects
        final ListView myListView = (ListView) findViewById(R.id.myListView);
        final ArrayList<String> todoItems = new ArrayList<String>();
        final ArrayAdapter<String> aa;
        
        //Extracting the category sent by MainActivity 
        Intent iIntentToReceiveCategory = new Intent();
        String category = (String)getIntent().getStringExtra("category");
        Toast.makeText(this, category, Toast.LENGTH_SHORT).show();
        
        //Using the extracted category
        final TextView mTextView = (TextView) findViewById(R.id.category);
        mTextView.setText(category);
        
        
        List<Recipe> recipes;
        
        if(category.equals("All Recipe"))
        {
        	recipes = db.getAllRecipes();
        	
        }else{
        	
        	recipes = db.getRecipesByCategory(category);
        }
        
        //traverse list from database and add the right ones to the list.
     	for (Recipe cn : recipes) {
            todoItems.add(0,cn.getName());  
     		}
     	recipes = null;
        
        //Adding the the arraylist to the arrayadapter
        aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
        myListView.setAdapter(aa);
        
        //Adding click event
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
//				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) {
	        		Object o = myListView.getItemAtPosition(position);
	        		String selectedRecipe=(String)o;
    	            disp(selectedRecipe);
    	            displayRecipeDetailed(selectedRecipe);
    	            //TestDB(selectedRecipe);
				}  
        	}); 
    }
    private void disp(String str){
    	Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    private void displayRecipeDetailed(String recipe){
    	Intent iIntentToSendRecipe = new Intent();
    	iIntentToSendRecipe.putExtra("recipe", recipe);
		iIntentToSendRecipe.setClass(this, RecipeActivity.class);
		startActivity(iIntentToSendRecipe);
    }
    
    public void onClickModifyRecipe(View v){
    	Intent iIntentToCreateRecipe = new Intent();
    	iIntentToCreateRecipe.setClass(this, ModifyRecipeActivity.class);
    	iIntentToCreateRecipe.putExtra("modify", "CreateRecipe");
		startActivity(iIntentToCreateRecipe);
    }
}

